package com.redlion.object;

import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.util.debug.Debug;
import org.andengine.util.math.MathUtils;

import com.badlogic.gdx.math.Vector2;
import com.redlion.Game;

public class OriginSpace {

	public static float WORLD_WIDTH = Game.CAMERA_WIDTH * Game.PIXEL_TO_METER_RATIO;
	public static float WORLD_HEIGHT = Game.CAMERA_HEIGHT * Game.PIXEL_TO_METER_RATIO;
	private static int countX = 0;
	private static int countY = 0;

	public static Vector2 randomPosition() {
		return Vector2Pool.obtain(MathUtils.random(WORLD_WIDTH * 0.5f, WORLD_WIDTH) * (MathUtils.randomSign()),
				MathUtils.random(WORLD_HEIGHT * 0.5f, WORLD_HEIGHT) * (MathUtils.randomSign()));
	}

	public static Vector2 randomPosition(float offset) {
		offset = offset * Game.PIXEL_TO_METER_RATIO;
		float x = MathUtils.random(-WORLD_WIDTH * 0.2f, WORLD_WIDTH * 1.2f);
		float y;
		if (MathUtils.isInBounds(-offset, WORLD_WIDTH + offset, x)) {
			if (MathUtils.randomSign() > 0)
				y = MathUtils.random(WORLD_HEIGHT + offset, WORLD_HEIGHT * 1.2f);
			else
				y = MathUtils.random(-offset, -WORLD_HEIGHT * 1.2f);
		} else {
			y = MathUtils.random(-offset, WORLD_HEIGHT + offset);
		}
		// Debug.d("Temp", x + ":" + y);
		// return Vector2Pool.obtain(countX++, countY++);
		return Vector2Pool.obtain(x, y);
	}

	// public static Vector2 randomPosition(float offset) {
	// return Vector2Pool.obtain(16, 5);
	// }

	public static boolean lost(Vector2 position) {
		if (!MathUtils.isInBounds(-WORLD_WIDTH * 0.5f, WORLD_WIDTH * 1.5f, position.x))
			return true;
		if (!MathUtils.isInBounds(-WORLD_HEIGHT * 0.5f, WORLD_HEIGHT * 1.5f, position.y))
			return true;
		return false;
	}

	public static boolean lost(Vector2 position, float offset) {
		offset = offset * Game.PIXEL_TO_METER_RATIO;
		if (!MathUtils.isInBounds(-offset - WORLD_WIDTH * 0.5f, WORLD_WIDTH * 1.5f + offset, position.x))
			return false;
		if (!MathUtils.isInBounds(-offset - WORLD_HEIGHT * 0.5f, WORLD_HEIGHT * 1.5f + offset, position.y))
			return false;
		return true;
	}
}
